using UnityEngine;
using UnityEngine.Networking;

[RequireComponent(typeof(CharacterSystem))]
public class CharacterDriver : NetworkBehaviour
{
	[HideInInspector]
	public Seat DrivingSeat;

	[HideInInspector]
	public CharacterSystem character;

	public Vehicle CurrentVehicle;

	[HideInInspector]
	public bool NeedParachute;

	private static int kCmdCmdDrive;

	private static int kCmdCmdOutVehicle;

	private static int kRpcRpcOutVehicle;

	private static int kCmdCmdRequstToGetCar;

	private static int kRpcRpcCarCallback;

	private void Start()
	{
		character = GetComponent<CharacterSystem>();
	}

	public void Drive(Vector2 input, bool brake)
	{
		CallCmdDrive(input, brake);
	}

	[Command(channel = 1)]
	private void CmdDrive(Vector2 input, bool brake)
	{
		if ((bool)DrivingSeat && DrivingSeat.IsDriver && (bool)DrivingSeat.VehicleRoot)
		{
			DrivingSeat.VehicleRoot.Drive(new Vector2(input.x, input.y), brake);
		}
	}

	public void OutVehicle()
	{
		CallCmdOutVehicle();
	}

	[Command(channel = 0)]
	private void CmdOutVehicle()
	{
		if ((bool)base.transform.root && (bool)base.transform.root.GetComponent<Vehicle>())
		{
			base.transform.root.GetComponent<Vehicle>().GetOutTheVehicle(this);
			CallRpcOutVehicle();
			base.transform.SendMessageUpwards("OnOutOfVehicle", NeedParachute, SendMessageOptions.DontRequireReceiver);
		}
	}

	[ClientRpc(channel = 0)]
	private void RpcOutVehicle()
	{
		NoVehicle();
	}

	public void NoVehicle()
	{
		base.transform.parent = null;
		if ((bool)character && (bool)character.controller)
		{
			character.controller.enabled = true;
		}
		DrivingSeat = null;
		CurrentVehicle = null;
	}

	public void PickupCarCallback(Vehicle car)
	{
		CallCmdRequstToGetCar(car.netId);
	}

	[Command(channel = 0)]
	private void CmdRequstToGetCar(NetworkInstanceId carid)
	{
		GameObject gameObject = ClientScene.FindLocalObject(carid);
		Vehicle component = gameObject.GetComponent<Vehicle>();
		if ((bool)component)
		{
			int num = component.FindOpenSeatID();
			component.GetInTheVehicle(this, num);
			CallRpcCarCallback(carid, num);
		}
	}

	[ClientRpc(channel = 0)]
	private void RpcCarCallback(NetworkInstanceId carid, int seatid)
	{
		UnityEngine.Debug.Log("Found a vehicle netID(" + carid.ToString() + ") callback : Find open seat ID = " + seatid);
		if (seatid != -1)
		{
			GameObject gameObject = ClientScene.FindLocalObject(carid);
			if ((bool)gameObject)
			{
				Vehicle component = gameObject.GetComponent<Vehicle>();
				component.GetInTheVehicle(this, seatid);
			}
		}
	}

	private void UNetVersion()
	{
	}

	protected static void InvokeCmdCmdDrive(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdDrive called on client.");
		}
		else
		{
			((CharacterDriver)obj).CmdDrive(reader.ReadVector2(), reader.ReadBoolean());
		}
	}

	protected static void InvokeCmdCmdOutVehicle(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdOutVehicle called on client.");
		}
		else
		{
			((CharacterDriver)obj).CmdOutVehicle();
		}
	}

	protected static void InvokeCmdCmdRequstToGetCar(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdRequstToGetCar called on client.");
		}
		else
		{
			((CharacterDriver)obj).CmdRequstToGetCar(reader.ReadNetworkId());
		}
	}

	public void CallCmdDrive(Vector2 input, bool brake)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdDrive called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdDrive(input, brake);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdDrive);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.Write(input);
		networkWriter.Write(brake);
		SendCommandInternal(networkWriter, 1, "CmdDrive");
	}

	public void CallCmdOutVehicle()
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdOutVehicle called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdOutVehicle();
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdOutVehicle);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		SendCommandInternal(networkWriter, 0, "CmdOutVehicle");
	}

	public void CallCmdRequstToGetCar(NetworkInstanceId carid)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdRequstToGetCar called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdRequstToGetCar(carid);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdRequstToGetCar);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.Write(carid);
		SendCommandInternal(networkWriter, 0, "CmdRequstToGetCar");
	}

	protected static void InvokeRpcRpcOutVehicle(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("RPC RpcOutVehicle called on server.");
		}
		else
		{
			((CharacterDriver)obj).RpcOutVehicle();
		}
	}

	protected static void InvokeRpcRpcCarCallback(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("RPC RpcCarCallback called on server.");
		}
		else
		{
			((CharacterDriver)obj).RpcCarCallback(reader.ReadNetworkId(), (int)reader.ReadPackedUInt32());
		}
	}

	public void CallRpcOutVehicle()
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("RPC Function RpcOutVehicle called on client.");
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)2);
		networkWriter.WritePackedUInt32((uint)kRpcRpcOutVehicle);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		SendRPCInternal(networkWriter, 0, "RpcOutVehicle");
	}

	public void CallRpcCarCallback(NetworkInstanceId carid, int seatid)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("RPC Function RpcCarCallback called on client.");
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)2);
		networkWriter.WritePackedUInt32((uint)kRpcRpcCarCallback);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.Write(carid);
		networkWriter.WritePackedUInt32((uint)seatid);
		SendRPCInternal(networkWriter, 0, "RpcCarCallback");
	}

	static CharacterDriver()
	{
		kCmdCmdDrive = 1040839481;
		NetworkBehaviour.RegisterCommandDelegate(typeof(CharacterDriver), kCmdCmdDrive, InvokeCmdCmdDrive);
		kCmdCmdOutVehicle = 1178574767;
		NetworkBehaviour.RegisterCommandDelegate(typeof(CharacterDriver), kCmdCmdOutVehicle, InvokeCmdCmdOutVehicle);
		kCmdCmdRequstToGetCar = -1935816062;
		NetworkBehaviour.RegisterCommandDelegate(typeof(CharacterDriver), kCmdCmdRequstToGetCar, InvokeCmdCmdRequstToGetCar);
		kRpcRpcOutVehicle = -957648891;
		NetworkBehaviour.RegisterRpcDelegate(typeof(CharacterDriver), kRpcRpcOutVehicle, InvokeRpcRpcOutVehicle);
		kRpcRpcCarCallback = 1530021138;
		NetworkBehaviour.RegisterRpcDelegate(typeof(CharacterDriver), kRpcRpcCarCallback, InvokeRpcRpcCarCallback);
		NetworkCRC.RegisterBehaviour("CharacterDriver", 0);
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		bool result = default(bool);
		return result;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
	}
}
